package com.xjrsoft.module.customerTwo.barin.brain_merchant.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;
import java.util.List;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.secure.XjrUser;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.customerTwo.barin.brain_merchant.dto.TBrainMerchantDto;
import com.xjrsoft.module.customerTwo.barin.brain_merchant.dto.SaveTBrainMerchantFormDataDto;
import com.xjrsoft.module.customerTwo.barin.brain_merchant.vo.TBrainMerchantVo;
import com.xjrsoft.module.customerTwo.barin.brain_merchant.vo.TBrainMerchantListVo;
import com.xjrsoft.module.customerTwo.barin.brain_merchant.dto.TBrainMerchantListDto;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.FlowableUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customerTwo.barin.brain_merchant.entity.TBrainMerchant;
import com.xjrsoft.module.customerTwo.barin.brain_merchant.vo.TBrainMerchantVo;
import com.xjrsoft.module.customerTwo.barin.brain_merchant.service.ITBrainMerchantService;

/**
 *  控制器
 *
 * @author dongxinhang
 * @since 2024-04-29
 */
@RestController
@AllArgsConstructor
@RequestMapping("/brain_merchant")
@Api(value = "服务商信息维护", tags = "服务商信息维护接口")
public class TBrainMerchantController {


	private final ITBrainMerchantService tBrainMerchantService;


	/**
	 * 详情 
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入tBrainMerchant")
	@SaCheckPermission(value = {"brain_merchant:view", "brain_merchant:modify"}, mode = SaMode.OR)
	public Response<TBrainMerchantVo> getFormData(@PathVariable String id) {
		// 主表数据
		TBrainMerchant tBrainMerchant = tBrainMerchantService.getById(id);
		TBrainMerchantVo tBrainMerchantVo = BeanUtil.copy(tBrainMerchant, TBrainMerchantVo.class);
		return Response.ok(tBrainMerchantVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入tBrainMerchant")
	public Response<PageOutput<TBrainMerchantListVo>> getPageList(TBrainMerchantListDto listDto) {
		XjrUser currentUser = SecureUtil.getUser();
		System.out.println(new Gson().toJson(currentUser));
		IPage<TBrainMerchantListVo> page = tBrainMerchantService.queryPage(new Page(listDto.getLimit(),listDto.getSize()),listDto);
		List<TBrainMerchantListVo> records = page.getRecords();
		if(CollectionUtils.isNotEmpty(records)){
			// 转换列表数据
			DataTransUtil.transListShowData(records);
		}
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增 
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入tBrainMerchant")
	@SaCheckPermission("brain_merchant:add")
	public Response save(@RequestBody SaveTBrainMerchantFormDataDto formDto) {
		TBrainMerchantDto tBrainMerchantDto = formDto.getTBrainMerchantDto();
		TBrainMerchant tBrainMerchant = BeanUtil.copy(tBrainMerchantDto, TBrainMerchant.class);
		String dataId = tBrainMerchantService.addTBrainMerchant(tBrainMerchant);
		return dataId == null ? Response.notOk() : Response.ok(dataId);
	}

	/**
	 * 修改 
	 */
	@PutMapping("/{id}")
	@ApiOperation(value = "修改", notes = "传入tBrainMerchant")
	@SaCheckPermission("brain_merchant:modify")
	public Response update(@PathVariable String id, @RequestBody SaveTBrainMerchantFormDataDto formDto) {
		TBrainMerchantDto tBrainMerchantDto = formDto.getTBrainMerchantDto();
		TBrainMerchant tBrainMerchant = BeanUtil.copy(tBrainMerchantDto, TBrainMerchant.class);
		return Response.status(tBrainMerchantService.updateTBrainMerchant(id, tBrainMerchant));
	}



	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	@SaCheckPermission(value = {"brain_merchant:batchDelete", "brain_merchant:delete"}, mode = SaMode.OR)
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(tBrainMerchantService.delete(ids));
	}

	/**
	 * 审核
	 */
	@PutMapping("/audit")
	@ApiOperation(value = "审核", notes = "传入tBrainMerchant")
	@SaCheckPermission("brain_merchant:audit")
	public Response audit(@RequestParam("id") String id,@RequestParam("status") String status) {
		TBrainMerchant tBrainMerchant = new TBrainMerchant();
		tBrainMerchant.setFStatus(status);
		return Response.status(tBrainMerchantService.updateTBrainMerchant(id, tBrainMerchant));
	}
}
